package com.pine.sunshineelder.waiter.service.impl;

import static com.pine.sunshineelder.common.validator.ValidateHelper.$;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.ElderAideDao;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.ElderAideEntity;
import com.pine.sunshineelder.common.entity.ElderBedEntity;
import com.pine.sunshineelder.common.validator.ValidateHelper;
import com.pine.sunshineelder.waiter.ao.ElderAideAo;
import com.pine.sunshineelder.waiter.service.ElderAideService;
import com.pine.sunshineelder.waiter.vo.ElderAideVo;

@Service
public class ElderAideServiceImpl implements ElderAideService, StringHelper {
	@Autowired
	ElderAideDao elderAideDao;

	@Override
	public ElderAideVo addElderAide(ElderAideAo ao) throws Exception {
		// 验证参数
		// 非空
		String elderId = $("老人id", ao.getElderId());
		String aideId = $("护工id", ao.getAideId());
		String reason = $("原因", ao.getReason());
		String createUserId = $("创建人id", ao.getCreateUserId());

		// 执行业务逻辑

		ElderAideEntity u = new ElderAideEntity();
		u.setId(uuid());
		u.setElderId(ao.getElderId());
		u.setAideId(ao.getAideId());
		u.setReason(ao.getReason());
		u.setCreateTime(ao.getCreateTime());
		u.setEndTime(ao.getEndTime());
		u.setCreateUserId(ao.getCreateUserId());

		elderAideDao.insert(u);
		;

		// 组装业务逻辑返回
		ElderAideVo dto = new ElderAideVo();
		dto.setId(u.getId());
		dto.setElderId(u.getElderId());
		dto.setAideId(u.getAideId());
		dto.setReason(u.getReason());
		dto.setCreateTime(toString(u.getCreateTime()));
		dto.setEndTime(toString(u.getEndTime()));
		dto.setCreateUserId(u.getCreateUserId());

		return dto;
	}

	public static String toString(LocalDateTime time) {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
		return time.format(dtf);
	}

	public static String toString(LocalDate date) {
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return date.format(dtf);
	}

	@Override
	public void deleteElderAideById(String id) throws Exception {

		// 验证清理参数
		// 非空
		String id1 = ValidateHelper.$("id", id);

		// 执行业务逻辑

		elderAideDao.delete(id1);

	}

	
	@Override
	public ElderAideVo selectById(String id) throws Exception {
		String id1 = ValidateHelper.$("id", id);
		// 执行业务逻辑
     System.out.println("+++++++++++++++++++++++++");
		ElderAideEntity u1;
		u1 = elderAideDao.selectById(id1);
	
		ElderAideVo u = new ElderAideVo();
		u.setId(u1.getId());
		System.out.println("=====================");
		u.setElderId(u1.getElderId());
		u.setAideId(u1.getAideId());
		u.setReason(u1.getReason());
		u.setCreateTime(toString(u1.getCreateTime()));
		u.setEndTime(toString(u1.getEndTime()));
		u.setCreateUserId(u1.getCreateUserId());
		return u;
	}

	@Override
	public QueryResult queryByKey(String key, Integer pageNo, Integer pageSize) throws Exception {
		
		key = trimAsNull(key);
		if (key != null) {
			key = "%" + key + "%";
		}

		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 6;
		}
		List<ElderAideEntity> rows = elderAideDao.selectByKey(key, pageNo, pageSize);

		PageInfo pageInfo = new PageInfo(rows);

		int totalRows = (int) pageInfo.getTotal();

		QueryResult result = new QueryResult();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setTotalRows(totalRows);
		result.setRows(rows);

		return result;
	}

	@Override
	public ElderAideEntity modifyElderBed(String id, Map<String, String> ao) throws Exception {

		return null;
	}

}
